<?php

use yii\web\Response;

$params = array_merge(
	require __DIR__ . '/../../common/config/params.php',
	require __DIR__ . '/../../common/config/params-local.php',
	require __DIR__ . '/params.php',
	require __DIR__ . '/params-local.php'
);

return [
	'id' => 'app-api',
	'basePath' => dirname(__DIR__),
	'controllerNamespace' => 'api\controllers',
	'layout'              => false,
	'bootstrap'           => ['log'],
	'components' => [
		'request' => [
			// 'csrfParam' => '_csrf-api',
			// 'baseUrl'                => Yii::getAlias('@apiUrl'),
			'enableCookieValidation' => false,
			'enableCsrfValidation'   => false,
			'parsers' => [
				'application/json' => 'yii\web\JsonParser',
			]
		],


		'user'       => [
			'class'           => 'yii\web\User',
			'identityClass'   => 'api\models\UserModel',
			'enableAutoLogin' => false, //关闭自动登录
			'enableSession'   => false, //关闭session
			'loginUrl'        => null, //登录跳转地址为空
			//'on afterLogin' => ['api\events\AfterLoginEvent', 'handleDayData'],
		],
		//自定义response组件
		'response'   => [
			'class'         => 'yii\web\Response',
			'format'        => Response::FORMAT_JSON,
			'on beforeSend' => function ($event) {
				$response = $event->sender;
				$response->data = (array)$response->data;
				if ($response->data !== null && is_array($response->data)) {
					if ($response->isSuccessful === false) {
						$response->data = [
							'success' => $response->isSuccessful,
							'code'    => $response->data['status'],
							'message' => $response->data['message'],
							'data'    => $response->data,
						];
					} else {
						$response->data = [
							'success' => $response->isSuccessful,
							'code'    => 200,
							'message' => 'OK',
							'data'    => $response->data,
						];
					}

					$response->statusCode = 200;
					$response->format     = Response::FORMAT_JSON;
				}
			},
			'formatters' => [
				Response::FORMAT_JSON => [
					'class' => 'yii\web\JsonResponseFormatter',
					'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode
					'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
				],
			],
		],
		'log' => [
			'traceLevel' => YII_DEBUG ? 3 : 0,
			'targets' => [
				[
					'class' => 'yii\log\FileTarget',
					'levels' => ['error', 'warning'],
				],
			],
		],
		'errorHandler' => [
			'errorAction' => 'site/error',
		],
		'urlManager' => [
			'enablePrettyUrl' => true,
			'showScriptName' => false,
			'rules'               => [
				[
					'class'      => 'yii\rest\UrlRule',
					'controller' => ['User'],
					//'only' => ['index', 'view', 'delete'], //仅只支持该2个默认方法
					'pluralize'  => false, //是否启用复数形式，注意index的复数indices，我认为开启后不直观
					
				]
			],
		],

	],
	'params' => $params,
];
